//
//  detailCamereViewController.m
//  UI_xiangmu
//
//  Created by dllo on 15/4/25.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "detailCamereViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+WebCache.h"
#import "juishudetielCell.h"
#import "ClassifyCollectionReusableView.h"
#import "jishuModel.h"
#import "PlayCameraViewController.h"
@interface detailCamereViewController ()

@property(nonatomic,retain)NSMutableDictionary *mydic;

@property(nonatomic,retain)NSMutableArray *boarray;

@property(nonatomic,retain)NSMutableArray *jishuArr;

@property(nonatomic,assign)NSInteger sum;

@property(nonatomic,assign)NSInteger sum1;

@property(nonatomic,assign)NSInteger *canshu2;

@end

@implementation detailCamereViewController

-(void)data
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
   
    [manager GET:self.canshu parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        self.mydic = [NSMutableDictionary dictionary];
        self.mydic = responseObject;
        
        
        UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(2, 76, 80, 120)];
        NSString *str = [self.mydic objectForKey:@"cover"];
        NSURL *url = [NSURL URLWithString:str];
        [imageview sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"48.jpg"]];
        [self.view addSubview:imageview];
        [imageview release];
        UILabel *namelabel = [[UILabel alloc] initWithFrame:CGRectMake(86, 76, 200, 30)];
        [self.view addSubview:namelabel];
        namelabel.text = [self.mydic objectForKey:@"name"];
        UILabel *arealabel = [[UILabel alloc] initWithFrame:CGRectMake(86, 110, 100, 20)];
        [self.view addSubview:arealabel];
        [arealabel release];
        NSString *str1 = [self.mydic objectForKey:@"area"];
        NSString *str2 = [NSString stringWithFormat:@"地区:%@",str1];
        arealabel.text = str2;
        arealabel.alpha = 0.6;
        
        UILabel *directerlabel = [[UILabel alloc] initWithFrame:CGRectMake(190, 110, 80, 20)];
        [self.view addSubview:directerlabel];
        [directerlabel release];
        NSString *str3 = [self.mydic objectForKey:@"director"];
        directerlabel.alpha = 0.6;
        if ([str3 isEqual:@""]) {
            directerlabel.text = @"导演:暂无";
            
        }else{
            NSString *str4 = [NSString stringWithFormat:@"导演:%@",str3];
            directerlabel.text = str4;
        }
        UILabel *characterlabel = [[UILabel alloc] initWithFrame:CGRectMake(86, 130, 80, 20)];
        characterlabel.alpha = 0.6;
        [self.view addSubview:characterlabel];
        [characterlabel release];
        NSString *str5 = [self.mydic objectForKey:@"character"];
        if ([str5 isEqual:@""]) {
            characterlabel.text = @"主演:暂无";
        }else{
            characterlabel.text = [NSString stringWithFormat:@"主演:%@",str5];
        }
        UILabel *update_stringlabel = [[UILabel alloc] initWithFrame:CGRectMake(86, 150, 270, 20)];
        update_stringlabel.alpha = 0.6;
        [self.view addSubview:update_stringlabel];
        [update_stringlabel release];
        NSString *str6 = [self.mydic objectForKey:@"update_string"];
        NSString *str7 = [self.mydic objectForKey:@"cur_num"];
        NSString *str8 = [self.mydic objectForKey:@"is_over"];
        NSString *str9 = [self.mydic objectForKey:@"total_num"];
        if ([str8 isEqual:@"1"]) {
            update_stringlabel.text = [NSString stringWithFormat:@"更新时间:已完结(全%@集)",str9];
        }else{
            update_stringlabel.text = [NSString stringWithFormat:@"更新时间:%@(更新至%@集)",str6,str7];
        }
        UILabel *publish_timelabel = [[UILabel alloc] initWithFrame:CGRectMake(86, 170, 270, 20)];
        [self.view addSubview:publish_timelabel];
        [publish_timelabel release];
        publish_timelabel.alpha = 0.6;
        NSString *str10 = [self.mydic objectForKey:@"publish_time"];
        if ([str10 isEqual:@""]) {
            publish_timelabel.text = @"暂无";
        }else{
        NSString *str11 = [str10 substringToIndex:4];
        NSString *str12 = [NSString stringWithFormat:@"上映时间:%@",str11];
        publish_timelabel.text = str12;
        }
        UIImageView *imageview2 = [[UIImageView alloc] initWithFrame:CGRectMake(290, 78, 80, 50)];
        [self.view addSubview:imageview2];
        [imageview2 release];
        imageview2.image = [UIImage imageNamed:@"49.jpg"];
        
       UILabel *scorelabel = [[UILabel alloc] initWithFrame:CGRectMake(300, 128, 60, 10)];
        [self.view addSubview:scorelabel];
        [scorelabel release];
        NSString *str13 = [NSString stringWithFormat:@"%@分",[self.mydic objectForKey:@"score"]];
        scorelabel.text = str13;
        scorelabel.textColor = [UIColor redColor];
        scorelabel.textAlignment = 1;
        
        UIButton *bu1 = [UIButton buttonWithType:UIButtonTypeSystem];
        bu1.frame = CGRectMake(86, 190, 70, 30);
        bu1.backgroundColor = [UIColor orangeColor];
        [self.view addSubview:bu1];
        [bu1 setTitle:@"立即播放" forState:UIControlStateNormal];
        [bu1 addTarget:self action:@selector(click1:) forControlEvents:UIControlEventTouchUpInside];
        bu1.tintColor = [UIColor whiteColor];
        
        UIButton *bu2 = [UIButton buttonWithType:UIButtonTypeSystem];
        bu2.frame = CGRectMake(171, 190, 70, 30);
        bu2.backgroundColor = [UIColor orangeColor];
        [self.view addSubview:bu2];
        [bu2 setTitle:@"立即下载" forState:UIControlStateNormal];
        [bu2 addTarget:self action:@selector(click2:) forControlEvents:UIControlEventTouchUpInside];
        bu2.tintColor = [UIColor whiteColor];
        
        UILabel *bolabel = [[UILabel alloc] initWithFrame:CGRectMake(2, 270, 58, 20)];
        
        [self.view addSubview:bolabel];
        bolabel.text = @"播放源:";
        [bolabel release];
        
        self.boarray = [self.mydic objectForKey:@"from_site"];
       
        for (int i = 0; i < self.boarray.count; i++) {
            NSMutableDictionary *dic = self.boarray[i] ;
            NSString *str = [dic objectForKey:@"from"];
            UIButton *bu = [UIButton buttonWithType:UIButtonTypeSystem];
            [self.view addSubview:bu];
            bu.frame = CGRectMake(70+i*30, 260, 28, 30);
            if ([str isEqual:@"tudou"]) {
                [bu setBackgroundImage:[UIImage imageNamed:@"tudou.jpg"] forState:UIControlStateNormal];
            }else if([str isEqual:@"youku"]){
                [bu setBackgroundImage:[UIImage imageNamed:@"youku.jpg"] forState:UIControlStateNormal];
        }
            else if([str isEqual:@"56"]){
                [bu setBackgroundImage:[UIImage imageNamed:@"56.jpg"] forState:UIControlStateNormal];
            }else if([str isEqual:@"pps"]){
                [bu setBackgroundImage:[UIImage imageNamed:@"PPS.jpg"] forState:UIControlStateNormal];
            }else if([str isEqual:@"iqiyi"]){
                [bu setBackgroundImage:[UIImage imageNamed:@"aiqiyi.jpg"] forState:UIControlStateNormal];
            }else if([str isEqual:@"letv"]){
                [bu setBackgroundImage:[UIImage imageNamed:@"leshi.jpg"] forState:UIControlStateNormal];
            }else if([str isEqual:@"pptv"]){
                [bu setBackgroundImage:[UIImage imageNamed:@"pptv.jpg"] forState:UIControlStateNormal];
            }else if([str isEqual:@"sohu"]){
                [bu setBackgroundImage:[UIImage imageNamed:@"souhu.jpg"] forState:UIControlStateNormal];
            }else if([str isEqual:@"qq"]){
                [bu setBackgroundImage:[UIImage imageNamed:@"tengxun.jpg"] forState:UIControlStateNormal];
            }else if([str isEqual:@"xunlei"]){
                [bu setBackgroundImage:[UIImage imageNamed:@"xunlei.jpg"] forState:UIControlStateNormal];
            }
            bu.tag = 500+i;
            [bu addTarget:self action:@selector(bofangclick:) forControlEvents:UIControlEventTouchUpInside];
            
        }
        UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"简介",@"剧集"]];
        seg.frame = CGRectMake(-1, 312, 377, 40);
        [self.view addSubview:seg];
        seg.tag = 1001;
        [seg release];
        seg.tintColor = [UIColor orangeColor];
        [seg addTarget:self action:@selector(clickseg:) forControlEvents:UIControlEventValueChanged];
        
        UILabel *jianjielabel = [[UILabel alloc] initWithFrame:CGRectMake(2, 355, 371, 290)];
        [self.view addSubview:jianjielabel];
        jianjielabel.tag = 1007;
        jianjielabel.text = [self.mydic objectForKey:@"intro"];
        jianjielabel.numberOfLines = 0;
        [jianjielabel sizeToFit];
        
        UICollectionViewFlowLayout *flowlayout=[[UICollectionViewFlowLayout alloc]init];
        flowlayout.itemSize=CGSizeMake(72 , 40);
        //最小列间距
        flowlayout.minimumInteritemSpacing=1;
        //最小行间距
        flowlayout.minimumLineSpacing=5;
        flowlayout.scrollDirection=UICollectionViewScrollDirectionVertical;
        flowlayout.sectionInset=UIEdgeInsetsMake(2, 2, 2,2);
        self.myCollectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 385, 375, 220)  collectionViewLayout:flowlayout] ;
        self.myCollectionView.backgroundColor=[UIColor whiteColor];
        self.myCollectionView.dataSource=self;
        self.myCollectionView.delegate=self;
        self.myCollectionView.tag = 1008;
        //注册cell
        [self.myCollectionView registerClass:[juishudetielCell class] forCellWithReuseIdentifier:@"reuse1"];
        [self.view addSubview:self.myCollectionView];
        [self.myCollectionView release];

        
        [self.myCollectionView registerClass:[ClassifyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
        [self.view bringSubviewToFront:[self.view viewWithTag:1007]];
        
        seg.selectedSegmentIndex = 0;
        self.myCollectionView.hidden = YES;
        
        UICollectionViewFlowLayout *flowlayout2=[[UICollectionViewFlowLayout alloc]init];
        flowlayout2.itemSize=CGSizeMake(100 , 25);
        //最小列间距
        flowlayout2.minimumInteritemSpacing=1;
        //最小行间距
        flowlayout2.minimumLineSpacing=10;
        flowlayout2.scrollDirection=UICollectionViewScrollDirectionHorizontal;
        flowlayout2.sectionInset=UIEdgeInsetsMake(2, 2, 2,2);
        flowlayout2.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        UICollectionView *jishuview = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 355, 375, 30) collectionViewLayout:flowlayout2];
        jishuview.delegate = self;
        jishuview.dataSource = self;
        [self.view addSubview:jishuview];
        [jishuview release];
        [jishuview registerClass:[jishuModel class] forCellWithReuseIdentifier:@"reuse2"];
        jishuview.backgroundColor = [UIColor whiteColor];
        jishuview.hidden = YES;
        jishuview.tag = 1009;
        
        self.jishuArr = [self.mydic objectForKey:@"episode"];
        
        self.sum = self.jishuArr.count;

        NSLog(@"%@",self.canshu);
        self.canshu2++;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"解析错误");
    }];
}

-(void)clickseg:(UISegmentedControl *)seg
{
    if (seg.selectedSegmentIndex == 0) {
        [self.view bringSubviewToFront:[self.view viewWithTag:1007]];
         self.myCollectionView.hidden = YES;
        [self.view viewWithTag:1007].hidden = NO;
        [self.view viewWithTag:1009].hidden = YES;
        
    }else{
        [self.view bringSubviewToFront:[self.view viewWithTag:1008]];
        self.myCollectionView.hidden = NO;
        [self.view viewWithTag:1007].hidden = YES;
        [self.view viewWithTag:1009].hidden = NO;
    }
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{   if(collectionView == self.myCollectionView){
    if (self.sum %50 != 0) {
        if (self.sum / 50 > 0) {
            return 50;
        }else{
            return self.jishuArr.count % 50;
        }
    }else{
       return 50;
          }
    }
else{
    return self.jishuArr.count/50+1;
}
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{   if(collectionView == self.myCollectionView){
    juishudetielCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse1" forIndexPath:indexPath];
    
        cell.mylabel.text = [NSString stringWithFormat:@"%ld",indexPath.row+1+self.sum1*50];
        return cell;
    
}else{
    jishuModel *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse2" forIndexPath:indexPath];
    UIView* selectedBGView = [[UIView alloc] initWithFrame:cell.bounds];
    selectedBGView.backgroundColor = [UIColor orangeColor];
    cell.selectedBackgroundView = selectedBGView;
    
    if (indexPath.row == self.jishuArr.count/50) {
        NSString *str = [NSString stringWithFormat:@"%ld-%ld",indexPath.row*50+1,self.jishuArr.count];
        NSLog(@"%@",str);
        cell.bu.text = str;
        cell.bu.tag = 10000+indexPath.row;
        
        return cell;
    }
    else{
        NSString *str = [NSString stringWithFormat:@"%ld-%ld",indexPath.row*50+1,(indexPath.row+1)*50];
        cell.bu.text = str;
   
        cell.bu.tag = 10000+indexPath.row;
        
    return cell;
    }
}
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == [self.view viewWithTag:1009]) {
        NSInteger i = self.jishuArr.count/50;
        if (indexPath.row < i) {
            self.sum = 50;
            self.sum1 = indexPath.row;
        }else{
            self.sum = self.jishuArr.count%50;
            self.sum1 = indexPath.row;
        }
        [self.myCollectionView reloadData];
       
    }else{
        PlayCameraViewController *play = [[PlayCameraViewController alloc] init];
        NSMutableDictionary *dic = self.jishuArr[indexPath.row+self.sum1*50];
        play.str = [dic objectForKey:@"mUrl"];
        NSLog(@"%@",play.str);
        [self.navigationController pushViewController:play animated:YES];
        [play release];
    }
    
    
}






#pragma mark 点击播放器图片切换
-(void)bofangclick:(UIButton *)bu
{
    NSInteger i = bu.tag - 500;
    self.canshu = [self.boarray[i] objectForKey:@"id"];
    NSLog(@"%@",self.canshu);
    self.canshu = [NSString stringWithFormat:@"http://manhua.haomee.cn/?m=Api&ios=23&pad=0&a=getEP&id=%@&new=1",self.canshu];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:self.canshu parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        self.mydic = [NSMutableDictionary dictionary];
        self.mydic = responseObject;
        self.jishuArr = [self.mydic objectForKey:@"list"];
        NSLog(@"%@",self.jishuArr);
        self.sum = self.jishuArr.count;
       
        [self.myCollectionView reloadData];
       
        UICollectionView *view = (UICollectionView *)[self.view viewWithTag:1009];
        [view reloadData];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"解析失败");
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self data];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(-1, 250, 377, 60)];
    view.layer.borderWidth = 1;
    [self.view addSubview:view];
    [view release];
    view.tag = 1000;
    view.backgroundColor = [UIColor whiteColor];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
